# encoding: UTF-8
"""
条件库
"""
from base.baseCondiction import CondictionTemplate
import numpy as np


class BarHoldsCondiction(CondictionTemplate):
    """Bar持有周期 条件指标"""
    """
    指标: 1.持有10周期平仓
    """
    def __init__(self):

        super(BarHoldsCondiction, self).__init__()

        self.positionPeriod = 0

    def caculate(self):
        """计算"""
        """持仓周期判断"""
        if self.strategy.getTotalPosition()>0:
            self.positionPeriod +=1
            return

    def buy(self):
        """买入条件"""
        signal = True
        return signal

    def sell(self):
        """卖出平仓条件"""
        signal = False
        """
        持仓10个BAR
        """
        if self.positionPeriod >=self.param['period']:
            signal = True
            self.positionPeriod = 0
        return signal

    def short(self):
        """卖空条件"""
        signal = True
        return signal

    def cover(self):
        """买入平仓条件"""
        signal = False
        """
        持仓10个BAR
        """
        if self.positionPeriod >=self.param['period']:
            signal = True
            self.positionPeriod = 0
        return signal

    def getSignalStr(self):
        if len(self.hisBar) >0:
            return "datetime=%s positionPeriod=%d" % (self.hisBar[-1].datetime, self.positionPeriod)
        return ""



